<template>
  <!--
    父传子
        传
           子组件标签  :属性名="父组件属性"

        收
            子组件内
              props:{
                属性名:{
                    type:Object/Array..., [Object,Array]
                    default:基本数据类型直接写，复杂数据类型()=>{return 复杂数据类型},
                    required:true  // 必传
                    validator:(value)=>{
                        return boolean值
                        true:验证通过
                        false:验证失败，报错
                    }
                }
              }
        特点：单向数据流（栈不可修改，堆可以修改）
              复杂数据类型是可以修改的，只要不修改它的引用地址
 -->
  <div class="item">
    <div class="title">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <span class="options">
        <span>操作</span>
        <i class="el-icon-arrow-down" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="edit"
          >编辑部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('addShow', true, this.item, 'add')
          break
        case 'edit':
          this.$bus.$emit('addShow', true, this.item, 'edit')
          break
        case 'del':
          this.$confirm('您确定删除该项吗？', '提示')
            .then(async() => {
              await companyDepartmentDelete(this.item.id)
              this.$message.success('删除成功')
              // 刷新父级列表数据
              this.$emit('getData')
              // this.$parent.getData()
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  align-items: center;
  .title {
    flex: 1;
  }
  .manager {
    width: 120px;
  }
  .options {
    font-size: 16px;
  }
}
</style>
